public class MyLinkedList {
    static class ListNode{
        private final int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode last;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
            return;
        }
        this.last.next = node;
        node.prev = last;
        last = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = search(index);
        ListNode preCur = cur.prev;
        preCur.next = node;
        node.prev = preCur;
        node.next = cur;
        cur.prev = node;
    }
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new RuntimeException("index out of bound");
        }
    }
    private ListNode search(int index){
        ListNode cur = this.head;
        while(index > 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            return;
        }
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                //删除头结点
                if(cur == this.head){
                    this.head = this.head.next;
                    if(this.head == null){
                        this.last = null;
                    }else{
                        this.head.prev = null;
                    }
                }else{
                    //删除中间节点
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else{
                        //删除尾巴节点
                        cur.prev.next = null;
                        last = last.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            return;
        }
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                if(cur == this.head){
                    this.head = this.head.next;
                    if(this.head == null){
                        this.last = null;
                    }else{
                        this.head.prev = null;
                    }
                }else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else{
                        cur.prev.next = null;
                        this.last = cur.prev;
                    }
                }
                cur = cur.next;
            }else{
                cur = cur.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        if(this.head == null){
            return;
        }
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
//        this.head = null;
//        this.last = null;
        ListNode cur = this.head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }
}
